# 爬取高德地图POI

from urllib.parse import quote
from urllib import request
import json
import xlwt

# TODO 替换为上面申请的密钥
amap_web_key = '申请的密钥key'
poi_search_url = "http://restapi.amap.com/v3/place/text"
poi_boundary_url = "https://ditu.amap.com/detail/get/detail"
# from transCoordinateSystem import gcj02_to_wgs84

# TODO cityname为需要爬取的POI所属的城市名，nanning_areas为城市下面的所有区，classes为多个分类名集合. (中文名或者代码都可以，代码详见高德地图的POI分类编码表)
# 北京市（京）:11、天津市（津）:12、上海市（沪）:31、重庆市（渝）:50、河北省（冀）:13、河南省（豫）:41、云南省（云）:53
# 辽宁省（辽）:21、黑龙江省（黑）:23、湖南省（湘）:43、安徽省（皖）:34、山东省（鲁）:37、新疆维吾尔（新）:65
# 江苏省（苏）:32、浙江省（浙）:33、江西省（赣）:36、湖北省（鄂）:42、广西壮族（桂）:45、甘肃省（甘）:62
# 山西省（晋）:14、内蒙古（蒙）:15、陕西省（陕）:61、吉林省（吉）:22、福建省（闽）:35、贵州省（贵）:52
# 广东省（粤）:44、青海省（青）:63、西藏（藏）: 54、四川省（川）:51、宁夏回族（宁）:64、海南省（琼）:46
cityName = '宁夏回族'
pNumber = '64'

nanning_areas = ['兴庆区', '西夏区', '金凤区', '永宁县', '贺兰县', '灵武市', '大武口区', '惠农区', '平罗县', '利通区', '红寺堡区', '盐池县',
                 '同心县', '青铜峡市', '原州区', '西吉县', '隆德县', '泾源县', '彭阳县', '沙坡头区', '中宁县', '海原县']
classes = ['村庄']


# 根据城市名称和分类关键字获取poi数据
def getpois(cityname, keywords):
    i = 1
    poilist = []
    while True:  # 使用while循环不断分页获取数据
        result = getpoi_page(cityname, keywords, i)
        print(result)
        result = json.loads(result)  # 将字符串转换为json
        if result['count'] == '0':
            break
        hand(poilist, result)
        i = i + 1
    return poilist


# 数据写入excel
def write_to_excel(poilist, cityname, classfield):
    # 一个Workbook对象，这就相当于创建了一个Excel文件
    book = xlwt.Workbook(encoding='utf-8', style_compression=0)
    sheet = book.add_sheet(classfield, cell_overwrite_ok=True)

    # 第一行(列标题)
    sheet.write(0, 0, 'pname')
    sheet.write(0, 1, 'cityname')
    sheet.write(0, 2, 'address')
    sheet.write(0, 3, 'name')
    sheet.write(0, 4, 'lng')
    sheet.write(0, 5, 'lat')
    sheet.write(0, 6, 'type')
    sheet.write(0, 7, 'number')
    sheet.write(0, 8, 'count')

    for i in range(len(poilist)):
        location = poilist[i]['location']
        pname = poilist[i]['pname']
        cityname = poilist[i]['cityname']
        if ("address" in poilist[i]):
            address = poilist[i]['address']
        else:
            address = ""
        name = poilist[i]['name']
        lng = str(location).split(",")[0]
        lat = str(location).split(",")[1]
        typeName = poilist[i]['type']

        '''
        result = gcj02_to_wgs84(float(lng), float(lat))

        lng = result[0]
        lat = result[1]
        '''

        # 每一行写入
        sheet.write(i + 1, 0, pname)
        sheet.write(i + 1, 1, cityname)
        sheet.write(i + 1, 2, address)
        sheet.write(i + 1, 3, name)
        sheet.write(i + 1, 4, lng)
        sheet.write(i + 1, 5, lat)
        sheet.write(i + 1, 6, typeName)
        sheet.write(i + 1, 7, pNumber)
        sheet.write(i + 1, 8, 1)

    # 最后，将以上操作保存到指定的Excel文件中
    book.save(r'../' + cityName + "_" + classfield + '.xls')

# 数据写入txt


def write_to_txt(poilist, cityname, classfield):
    file_handle = open(cityName + "_" + classfield + '.txt', mode='w')

    # 第一行(列标题)
    file_handle.write(
        'pname,cityname,address,name,lng,lat,type,number,count\n')

    for i in range(len(poilist)):
        location = poilist[i]['location']
        pname = poilist[i]['pname']
        cityname = poilist[i]['cityname']
        if ("address" in poilist[i]):
            address = poilist[i]['address']
        else:
            address = ""
        name = poilist[i]['name']
        lng = str(location).split(",")[0]
        lat = str(location).split(",")[1]
        typeName = poilist[i]['type']

        '''
        result = gcj02_to_wgs84(float(lng), float(lat))

        lng = result[0]
        lat = result[1]
        '''

        # 每一行写入
        file_handle.write(str(pname)+','+str(cityname)+','+str(address)+','+str(
            name)+','+str(lng)+','+str(lat)+','+str(typeName)+','+str(pNumber)+',1\n')

    # 最后，关闭
    file_handle.close()

# 将返回的poi数据装入集合返回


def hand(poilist, result):
    # result = json.loads(result)  # 将字符串转换为json
    pois = result['pois']
    for i in range(len(pois)):
        poilist.append(pois[i])


# 单页获取pois
def getpoi_page(cityname, keywords, page):
    req_url = poi_search_url + "?key=" + amap_web_key + '&extensions=all&keywords=' + quote(
        keywords) + '&city=' + quote(cityname) + '&citylimit=true' + '&offset=25' + '&page=' + str(
        page) + '&output=json'
    data = ''
    with request.urlopen(req_url) as f:
        data = f.read()
        data = data.decode('utf-8')
    return data


for clas in classes:
    classes_all_pois = []
    try:
        for area in nanning_areas:
            pois_area = getpois(area, clas)
            print('当前城区：' + str(area) + ', 分类：' + str(clas) +
                  ", 总的有" + str(len(pois_area)) + "条数据")
            classes_all_pois.extend(pois_area)
        print("所有城区的数据汇总，总数为：" + str(len(classes_all_pois)))
        write_to_excel(classes_all_pois, cityName, clas)
    finally:
        write_to_txt(classes_all_pois, cityName, clas)

    print('================分类：' + str(clas) + "写入成功")
